package com.shizhanzhe.qdd.home.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gyf.barlibrary.ImmersionBar;
import com.shizhanzhe.qdd.R;
import com.shizhanzhe.qdd.base.UserBean;
import com.shizhanzhe.qdd.home.dialog.ExitDialog;
import com.shizhanzhe.qdd.utils.DataCleanManager;
import com.shizhanzhe.qdd.utils.SharedPreferencesUtils;
import com.tencent.bugly.beta.Beta;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by zz9527 on 2018/11/7.
 */
public class SettingActivity extends Activity {
    @BindView(R.id.version)
    TextView version;
    @BindView(R.id.tuichuzhanghaoarea)
    RelativeLayout tuichuzhanghaoarea;
    @BindView(R.id.tv_cache)
    TextView tvCache;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        ImmersionBar.with(this).statusBarColor(R.color.them)
                .fitsSystemWindows(true).init();
        initData();
    }

    @SuppressLint("SetTextI18n")
    private void initData() {
        version.setText("V" + getVersionName(this));
        try {
            tvCache.setText(DataCleanManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (UserBean.uid == 0) {
            tuichuzhanghaoarea.setVisibility(View.INVISIBLE);
        } else {
            tuichuzhanghaoarea.setVisibility(View.VISIBLE);
        }
    }

    /**
     * @return 当前应用的版本号
     */
    public String getVersionName(Context context) {
        try {
            PackageManager manager = context.getPackageManager();
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    @OnClick({R.id.back, R.id.xiugaimimaarea, R.id.qingchuhuancun, R.id.update, R.id.tuichuzhanghaoarea, R.id.guanyu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.xiugaimimaarea:
                if (UserBean.uid == 0) {
                    startActivity(new Intent(this, LoginActivity.class));
                } else {
                    startActivity(new Intent(this, ChangePsdActivity.class));
                }
                break;
            case R.id.qingchuhuancun:
                final ExitDialog mdelDialog = new ExitDialog(SettingActivity.this);
                mdelDialog.setExitListener(new ExitDialog.ExitListener() {
                    @Override
                    public void chooseTrue() {
                        //确定清除
                        mdelDialog.dismiss();
                        DataCleanManager.clearAllCache(getApplicationContext());
                        Toast.makeText(getApplicationContext(), "清除完成", Toast.LENGTH_SHORT).show();
                        try {
                            tvCache.setText(DataCleanManager.getTotalCacheSize(SettingActivity.this));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void chooseFalse() {
                        //取消
                        mdelDialog.dismiss();
                    }
                });
                mdelDialog.show();
                break;
            case R.id.guanyu:
                if (UserBean.uid == 0) {
                    startActivity(new Intent(this, LoginActivity.class));
                } else {
                    startActivity(new Intent(this, AboutActivity.class));
                }
                break;
            case R.id.update:
                Beta.checkUpgrade();
                break;
            case R.id.tuichuzhanghaoarea:
                final ExitDialog mExitDialog = new ExitDialog(SettingActivity.this);
                mExitDialog.setExitListener(new ExitDialog.ExitListener() {
                    @Override
                    public void chooseTrue() {
                        mExitDialog.dismiss();
                    }

                    @Override
                    public void chooseFalse() {
                        //退出
                        SharedPreferencesUtils.clearAll(getApplicationContext());
                        SharedPreferencesUtils.setDevice(getApplicationContext());
                        Intent intent = new Intent(SettingActivity.this, SplashActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
//                        setMsg();
                    }
                });
                mExitDialog.show();
                mExitDialog.setContent(UserBean.balance);
                break;
            default:
        }
    }
}
